package sp.servlet;

import com.alibaba.fastjson.JSON;
import sp.service.ClassesService;
import sp.service.ClassesServiceImpl;
import sp.dao.Page;
import sp.entity.Classes;
import sp.util.ParamUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/classes")
public class ClassesServlet extends HttpServlet {
    ClassesService classesService = new ClassesServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        String classId = req.getParameter("classId");
        String className = req.getParameter("className");
        int page = ParamUtil.parseInt(req.getParameter("page"),1);
        int rows = ParamUtil.parseInt(req.getParameter("rows"),2);
        Page<Classes> pageBean = classesService.findPage(classId,className,page,rows);
        List<Classes> allType = classesService.findAllType();
        Map map = new HashMap();
        map.put("data",pageBean);
        map.put("types",allType);
        resp.getWriter().write(JSON.toJSONString(map));
        resp.getWriter().flush();
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        String classId = req.getParameter("classId");
        String resultFormat = "{\"state\":\"%b\",\"error\":\"%s\"}";
        String result = "";
        try {
            classesService.delete(classId);
            result = String.format(resultFormat,true,"");
        } catch (RuntimeException e){
            result  = String.format(resultFormat,false,e.getMessage());
        }
        resp.getWriter().write(result);
        resp.flushBuffer();
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        String classId = req.getParameter("classId");
        String className = req.getParameter("className");
        Classes cls = new Classes(classId,className);
        String resultFormat = "{\"state\":\"%b\",\"error\":\"%s\"}";
        String result = "";
        if(classId!=null&&classId.trim().length()>0){
            try {
                classesService.addClass(cls);
                result = String.format(resultFormat,true,"");
            } catch (RuntimeException e){
                result  = String.format(resultFormat,false,e.getMessage());
            }
        }
        else result  = String.format(resultFormat,false,"");
        resp.getWriter().write(result);
        resp.flushBuffer();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        String classId = req.getParameter("classId");
        String className = req.getParameter("className");
        Classes cls = new Classes(classId,className);
        String resultFormat = "{\"state\":\"%b\",\"error\":\"%s\"}";
        String result = "";
        if(classId!=null&&classId.trim().length()>0){
            try {
                classesService.updateClass(cls);
                result = String.format(resultFormat,true,"");
            } catch (RuntimeException e){
                result  = String.format(resultFormat,false,e.getMessage());
            }
        }
        else result  = String.format(resultFormat,false,"");
        resp.getWriter().write(result);
        resp.flushBuffer();
    }

    public static void main(String[] args) {

    }
}
